package com.mf.all.controller.basic;


import com.mf.all.entity.req.publicReq.updateStatus.ReqPublicIntIdUpdateStatus;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.service.ServiceUser;
import com.mf.all.utils.UtilController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoDept;
import com.mf.all.entity.req.basic.ReqDept;
import com.mf.all.service.ServiceDept;
import com.mf.all.entity.req.search.basic.SearchReqDept;

import java.util.List;

/**
 * 部门表
 *
 * @date 2025-04-02 22:36:46
 */
@RestController
@RequestMapping("dept")
@Tag(name = "部门表")
public class ControllerDept {
    @Resource
    private ServiceDept servicedept;

    @Resource
    private ServiceUser serviceUser;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("dept:search")
    public Result<Page<DtoDept>> findPage(@RequestBody SearchReqDept searchReqDept) {
        return Result.success(servicedept.findPageByParams(searchReqDept));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("dept:search")
    public Result<List<DtoDept>> list(@RequestBody SearchReqDept searchReqDept) {
        return Result.success(servicedept.findListByParams(searchReqDept));
    }

    @Operation(summary = "树形结构列表查询")
    @PostMapping("/listTree")
    @SaCheckPermission("dept:search")
    public Result<List<DtoDept>> listTree() {
        return Result.success(servicedept.listTree());
    }


    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("dept:save")
    @Transactional
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqDept reqDept) {
        commitValidate(reqDept);
        servicedept.save(UtilController.setBo(reqDept, DtoDept.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("dept:update")
    @Transactional
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqDept reqDept) {
        commitValidate(reqDept);
        servicedept.updateById(UtilController.setBo(reqDept, DtoDept.class));
        return Result.success();
    }

    @Operation(summary = "修改状态")
    @PostMapping("/updateStatus")
    @SaCheckPermission("dept:update")
    public Result<Void> update(@Validated @RequestBody ReqPublicIntIdUpdateStatus reqDept) {
        servicedept.updateStatus(UtilController.setBo(reqDept, DtoDept.class));
        return Result.success();
    }

    /**
     * 提交验证
     */
    @Transactional
    public void commitValidate(ReqDept reqDept) {
        serviceUser.isExistById(reqDept.getPrincipalId());
        if (reqDept.getPid() != null && reqDept.getPid() > 0) {
            servicedept.isExistById(reqDept.getPid());
        }
    }

    @Operation(summary = "删除")
    @GetMapping("/delete/{id}")
    @SaCheckPermission("dept:delete")
    public Result<Void> delete(@PathVariable Integer id) {
        servicedept.deleteById(id);
        return Result.success();
    }
}
